//
//  RightShopTableViewCell.m
//  FangXinCai
//
//  Created by ios-1 on 2018/1/29.
//  Copyright © 2018年 ios-1. All rights reserved.
//

#import "RightShopTableViewCell.h"

@implementation RightShopTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
  
    self.shopName.frame = CGRectMake(10, 10, kScreenWidth-100, 20);
    self.shopPrice.frame = CGRectMake(10, 30, kScreenWidth-100, 20);
    self.spaceline.hidden = YES;
    self.bottomline.hidden = NO;

    self.inPutTextField.borderStyle = UITextBorderStyleNone;
    self.inPutTextField.keyboardType = UIKeyboardTypeDecimalPad;
    self.inPutTextField.delegate = self;
    self.priceTF.delegate = self;
    self.backView.hidden = YES;

    //加购物车
    [self.addActiveBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 35)];
    [self.addActiveBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 35, 0, 0)];
    [self.addActiveBtn setImage:[UIImage imageNamed:@"圆加"] forState:UIControlStateNormal];

    [self.reduceBtn addTarget:self action:@selector(reduceClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.addBtn addTarget:self action:@selector(addClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.addActiveBtn addTarget:self action:@selector(addActiveClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)refreshData:(GoodListData*)data ISOut:(BOOL)is_out;
{
    self.shopmodel = data;
    self.goods_id = [NSString stringWithFormat:@"%zd",data.goodsId];
    
    self.shopName.text = [NSString stringWithFormat:@"%@(%@)",data.goodsName,data.unit];
    
    NSString *unit = ([data.unit isEqualToString:@"<null>"] || data.unit == nil)?@"":data.unit;
    if(is_out)//出货
    {
        //self.shopPrice.text = [NSString stringWithFormat:@"￥%.2f/%@",data.salePrice,unit];
        self.priceTF.text = [NSString stringWithFormat:@"%.2f",data.salePrice];
        //self.shopPrice.text = [NSString stringWithFormat:@"/%@",data.unit];
        
        
    }else{//进货
       // self.shopPrice.text = [NSString stringWithFormat:@"￥%.2f/%@",data.price,unit];
        
        self.priceTF.text = [NSString stringWithFormat:@"%.2f",data.price];
        //self.shopPrice.text = [NSString stringWithFormat:@"/%@",data.unit];
    }
    self.priceTF.tag = data.ROW_NUMBER.integerValue;
    
    self.inPutTextField.text = [NSString stringWithFormat:@"%.2f",data.quantity];
    
    //加购物车
    [self.addActiveBtn setTitle:@"" forState:UIControlStateNormal];
    self.addActiveBtn.backgroundColor = [UIColor clearColor];
    
    [self.addActiveBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 35)];
    [self.addActiveBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 35, 0, 0)];
    [self.addActiveBtn setImage:[UIImage imageNamed:@"圆加"] forState:UIControlStateNormal];
    
    //加减商品按钮是否隐藏
    if(self.inPutTextField.text.integerValue > 0)
    {
        self.backView.hidden = NO;
        self.addActiveBtn.hidden = YES;
    }else{
        self.backView.hidden = NO;
        self.addActiveBtn.hidden = YES;
    }
}

//- (void)refreshData:(GoodsShopData*)data;
//{
//    self.shopmodel = data;
//    self.spec_key = data.spec_key;
//    self.goods_id = data.goods_id;
//    
//    self.shopName.text = [NSString stringWithFormat:@"%@",data.key_name];
//    self.shopPrice.text = [NSString stringWithFormat:@"￥%@/%@",data.price,data.goods_unit];
//    self.inPutTextField.text = [NSString stringWithFormat:@"%@",data.goods_num];
//    
//    //加购物车
//    [self.addActiveBtn setTitle:@"" forState:UIControlStateNormal];
//    self.addActiveBtn.backgroundColor = [UIColor clearColor];
//    
//    [self.addActiveBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 35)];
//    [self.addActiveBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 35, 0, 0)];
//    [self.addActiveBtn setImage:[UIImage imageNamed:@"圆加"] forState:UIControlStateNormal];
//    
//    //加减商品按钮是否隐藏
//    if(self.inPutTextField.text.integerValue > 0)
//    {
//        self.backView.hidden = NO;
//        self.addActiveBtn.hidden = YES;
//    }else{
//        self.backView.hidden = YES;
//        self.addActiveBtn.hidden = NO;
//    }
//}
//
//- (void)refreshTextData:(GoodsShopData*)data;
//{
//    self.shopmodel = data;
//    self.spec_key = data.spec_key;
//    self.goods_id = data.goods_id;
//    
//    self.shopName.text = [NSString stringWithFormat:@"%@",data.goods_name];
//    self.shopPrice.text = [NSString stringWithFormat:@"￥%@/%@",data.price,data.goods_unit];
//    self.inPutTextField.text = [NSString stringWithFormat:@"%@",data.goods_num];
//    
//    //加购物车
//    [self.addActiveBtn setTitle:@"" forState:UIControlStateNormal];
//    self.addActiveBtn.backgroundColor = [UIColor clearColor];
//    
//    [self.addActiveBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 35)];
//    [self.addActiveBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 35, 0, 0)];
//    [self.addActiveBtn setImage:[UIImage imageNamed:@"圆加"] forState:UIControlStateNormal];
//    
//    //加减商品按钮是否隐藏
//    if(self.inPutTextField.text.integerValue > 0)
//    {
//        self.backView.hidden = NO;
//        self.addActiveBtn.hidden = YES;
//    }else{
//        self.backView.hidden = YES;
//        self.addActiveBtn.hidden = NO;
//    }
//}
//
//- (void)refreshSpecData:(GoodsShopInfoModel*)data Speckey:(NSString*)speckey;
//{
//    self.goods_id = data.goods_id;
//    self.spec_key = speckey;
//    
//    NSString * shopname = [NSString stringWithFormat:@"￥%.2f",data.price.floatValue];
//    if(data.goods_unit.length >0)
//    {
//        shopname = [NSString stringWithFormat:@"￥%.2f/%@",data.price.floatValue,data.goods_unit];
//    }
//    NSString * shopprice = [NSString stringWithFormat:@"￥%.2f",data.market_price.floatValue];
//    
//    CGFloat shopnameWidth = [self getRowHeight:shopname fontSize:KZOOM6pt(30)];
//    CGFloat shoppriceWidth = [self getRowHeight:shopprice fontSize:KZOOM6pt(30)];
//    
//    self.shopName.frame = CGRectMake(10, 20, shopnameWidth, 20);
//    self.shopName.textColor = [UIColor redColor];
//    
//    self.shopPrice.frame = CGRectMake(CGRectGetMaxX(self.shopName.frame), 21, shoppriceWidth, 20);
//    self.shopPrice.textColor = KGrayColor;
//    
//    self.spaceline.frame = CGRectMake(CGRectGetMaxX(self.shopName.frame), 29, shoppriceWidth-20, 1);
//    self.spaceline.center = self.shopPrice.center;
//    self.spaceline.hidden = NO;
//    
//    self.bottomline.hidden = YES;
//    
//    self.shopName.text = shopname;
//    self.shopPrice.text = shopprice;
//    self.inPutTextField.text = [NSString stringWithFormat:@"%@",data.goods_num];
//    
//    //加购物车
//    [self.addActiveBtn setTitle:@"" forState:UIControlStateNormal];
//    self.addActiveBtn.backgroundColor = [UIColor clearColor];
//    
//    [self.addActiveBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 35)];
//    [self.addActiveBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 35, 0, 0)];
//    [self.addActiveBtn setImage:[UIImage imageNamed:@"圆加"] forState:UIControlStateNormal];
//    
//    //加减商品按钮是否隐藏
//    if(self.inPutTextField.text.integerValue > 0)
//    {
//        self.backView.hidden = NO;
//        self.addActiveBtn.hidden = YES;
//    }else{
//        self.backView.hidden = YES;
//        self.addActiveBtn.hidden = NO;
//    }
//}
- (void)addActiveClick:(UIButton*)sender
{
    NSString *goods_num = [NSString stringWithFormat:@"%zd",self.inPutTextField.text.integerValue+1];
   
    [self AddReduiceCartShop:goods_num Add:YES];
}
- (void)reduceClick:(UIButton*)sender
{
    
    if (self.inPutTextField.text.integerValue-1<0) {
         [self AddReduiceCartShop:@"0" Add:NO];
    }else{
        
        NSString *goods_num = [NSString stringWithFormat:@"%.4f",self.inPutTextField.text.doubleValue-1];
        
        [self AddReduiceCartShop:goods_num Add:NO];
    }
    
}
- (void)addClick:(UIButton*)sender
{
    
    
    NSString *goods_num = [NSString stringWithFormat:@"%.4f",self.inPutTextField.text.doubleValue+1];
    
    [self AddReduiceCartShop:goods_num Add:YES];
}



- (BOOL)textFieldShouldEndEditing:(UITextField *)textField;
{
    
    if (textField == self.inPutTextField) {
        if(textField.text.integerValue == 0)
        {
            // textField.text = @"1";
            self.shopmodel.quantity = textField.text.doubleValue;
            
            
        }else if(textField.text.integerValue >= 1){
            self.shopmodel.quantity = textField.text.doubleValue;
        }
        
        
        
        if(self.addCartBlock1)
        {
            self.addCartBlock1(textField.text.doubleValue,YES);
        }
    }
   
    return YES;
}

-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField ==self.priceTF) {
       self.priceChangeBlock1(textField.text.doubleValue,textField.tag);
    }
    
    
    
}

- (void)AddReduiceCartShop:(NSString*)good_num Add:(BOOL)add


{
    
    
    
    self.inPutTextField.text = good_num;
    
    if(good_num.integerValue == 0)
    {
        self.addActiveBtn.hidden = NO;
        self.backView.hidden = YES;
    }else if(good_num.integerValue > 0){
        self.addActiveBtn.hidden = YES;
        self.backView.hidden = NO;
        
    }
    
    //加购物车成功的回调
    if (self.backView.hidden) {
        if(self.addCartBlock1)
        {
            self.addCartBlock1(0,add);
        }
    }else{
        
        if(self.addCartBlock1)
        {
            self.addCartBlock1(good_num.doubleValue,add);
        }
        
    }
   
}

@end
